package com.ruoyi.system.service.impl;

import java.util.*;

import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.uuid.Seq;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.mapper.SysMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.common.core.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.system.api.domain.SysSerialOption;
import com.ruoyi.system.mapper.SysSerialConfigMapper;
import com.ruoyi.system.api.domain.SysSerialConfig;
import com.ruoyi.system.service.ISysSerialConfigService;

/**
 * 流水号字段配置Service业务层处理
 *
 * @author ruoyi
 * @date 2023-04-14
 */
@Service
public class SysSerialConfigServiceImpl implements ISysSerialConfigService {

    @Autowired
    private SysSerialConfigMapper sysSerialConfigMapper;

    @Autowired
    private SysMapper sysMapper;

    /**
     * 查询流水号字段配置
     *
     * @param configId 流水号字段配置主键
     * @return 流水号字段配置
     */
    @Override
    public SysSerialConfig selectSysSerialConfigByConfigId(Long configId) {
        SysSerialConfig sysSerialConfig = sysSerialConfigMapper.selectSysSerialConfigByConfigId(configId);
        if (StringUtils.isNotEmpty(sysSerialConfig.getRelConfigId())) {
            String[] relConfigIds = sysSerialConfig.getRelConfigId().split(",");
            Long[] relConfigIds2 = new Long[relConfigIds.length];
            for (int i = 0; i < relConfigIds.length; i++) {
                relConfigIds2[i] = Long.valueOf(relConfigIds[i]);
            }
            sysSerialConfig.setRelConfigIds(relConfigIds2);
        }
        return sysSerialConfig;
    }

    /**
     * 查询流水号字段配置列表
     *
     * @param sysSerialConfig 流水号字段配置
     * @return 流水号字段配置
     */
    @Override
    public List<SysSerialConfig> selectSysSerialConfigList(SysSerialConfig sysSerialConfig) {
        List<SysSerialConfig> sysSerialConfigList = sysSerialConfigMapper.selectSysSerialConfigList(sysSerialConfig);
        for (SysSerialConfig serialConfig : sysSerialConfigList) {
            if (StringUtils.isNotEmpty(serialConfig.getRelConfigId())) {
                //string[]转换为Long[]
                String[] relConfigIds = StringUtils.split(serialConfig.getRelConfigId(), ",");
                Long[] relConfigIds2 = new Long[relConfigIds.length];
                for (int i = 0; i < relConfigIds.length; i++) {
                    relConfigIds2[i] = Long.valueOf(relConfigIds[i]);
                }
                serialConfig.setRelConfigIds(relConfigIds2);
            }
            if (serialConfig.getConfigType() == 4) {
                List<SysSerialOption> optionList = sysSerialConfigMapper.selectSysSerialConfigByConfigId(serialConfig.getConfigId()).getSysSerialOptionList();
                serialConfig.setSysSerialOptionList(optionList);
            } else if (serialConfig.getConfigType() == 3 && serialConfig.getWriteType() != null && serialConfig.getWriteType() == 1) {
                String congfigValue = DateUtils.parseDateToStr(serialConfig.getConfigName(), new Date());
                serialConfig.setConfigValue(congfigValue);
            } else if (serialConfig.getConfigType() == 1) {
                Map paramMap = sysSerialConfig.getParams();
                if (StringUtils.isNotEmpty(paramMap)) {
                    String sql = StringUtils.getExecuteSql(serialConfig.getConfigValue(), paramMap);
                    List<Map<String,Object>> optionMapList = sysMapper.select(sql);
                    List<SysSerialOption> optionList = new ArrayList<>(optionMapList.size());
                    for(Map<String,Object> optionMap : optionMapList){
                        SysSerialOption option = new SysSerialOption();
                        if(optionMap.get("value") != null) {
                            option.setValue(String.valueOf(optionMap.get("value")));
                        }
                        if(optionMap.get("label") != null) {
                            option.setLabel(String.valueOf(optionMap.get("label")));
                        }
                        if(optionMap.get("value") != null || optionMap.get("label") != null){
                            optionList.add(option);
                        }
                    }
                    serialConfig.setSysSerialOptionList(optionList);
                }
            }
        }
        return sysSerialConfigList;
    }

    /**
     * 新增流水号字段配置
     *
     * @param sysSerialConfig 流水号字段配置
     * @return 结果
     */
    @Transactional
    @Override
    public Long insertSysSerialConfig(SysSerialConfig sysSerialConfig) {
        Long orgId = SecurityUtils.getOrgId();
        sysSerialConfig.setOrgId(orgId);
        if (StringUtils.isNotEmpty(sysSerialConfig.getRelConfigIds())) {
            String relConfigId = StringUtils.join(sysSerialConfig.getRelConfigIds(), ",");
            sysSerialConfig.setRelConfigId(relConfigId);
        }
        if (sysSerialConfig.getConfigType() == 3) {
            String configName = sysSerialConfig.getConfigName().replace("DD", "dd").replace("mm", "MM");
            sysSerialConfig.setConfigName(configName);
        }
        if (StringUtils.isNotNull(sysSerialConfig.getConfigId())) {
            SysSerialConfig sysSerialConfig1 = sysSerialConfigMapper.selectSysSerialConfigByConfigId(sysSerialConfig.getConfigId());
            sysSerialConfigMapper.deleteSysSerialOptionByConfigId(sysSerialConfig.getConfigId());
            sysSerialConfigMapper.updateSysSerialConfig(sysSerialConfig);
            if (sysSerialConfig.getOrderNum().intValue() != sysSerialConfig1.getOrderNum().intValue()) {
                updateSysSerialOrderNum(sysSerialConfig);
            }
        } else {
            Integer maxOrderNum = sysSerialConfigMapper.selectMaxOrderNum(sysSerialConfig);
            if (maxOrderNum == null) {
                sysSerialConfig.setOrderNum(1);
            } else if (sysSerialConfig.getOrderNum() >= maxOrderNum) {
                sysSerialConfig.setOrderNum(++maxOrderNum);
            }
            sysSerialConfig.setConfigId(Seq.getSeqId());
            sysSerialConfigMapper.insertSysSerialConfig(sysSerialConfig);
            if (maxOrderNum != null && sysSerialConfig.getOrderNum() < maxOrderNum) {
                updateSysSerialOrderNum(sysSerialConfig);
            }
        }
        if (sysSerialConfig.getConfigType() == 4) {
            insertSysSerialOption(sysSerialConfig);
        }
        return sysSerialConfig.getConfigId();
    }

    /**
     * 更新当前编号的顺序号从1开始递增更新为连续的顺序号。
     *
     * @param sysSerialConfig
     * @return
     */
    private void updateSysSerialOrderNum(SysSerialConfig sysSerialConfig) {
        List<Long> configList = sysSerialConfigMapper.selectConfigIdBySerialId(sysSerialConfig.getSerialId());
        int i = 1;
        for (Long configId : configList) {
            SysSerialConfig config = new SysSerialConfig();
            config.setConfigId(configId);
            config.setOrderNum(i++);
            if (sysSerialConfigMapper.updateSysSerialConfig(config) == 0) {
                throw new ServiceException("保存失败，请联系管理员！");
            }
        }
    }

    /**
     * 批量删除流水号字段配置
     *
     * @param configIds 需要删除的流水号字段配置主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteSysSerialConfigByConfigIds(Long[] configIds) {
        sysSerialConfigMapper.deleteSysSerialOptionByConfigIds(configIds);
        return sysSerialConfigMapper.deleteSysSerialConfigByConfigIds(configIds);
    }

    /**
     * 删除流水号字段配置信息
     *
     * @param configId 流水号字段配置主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteSysSerialConfigByConfigId(Long configId) {
        sysSerialConfigMapper.deleteSysSerialOptionByConfigId(configId);
        return sysSerialConfigMapper.deleteSysSerialConfigByConfigId(configId);
    }

    /**
     * 新增流水号下拉数据信息
     *
     * @param sysSerialConfig 流水号字段配置对象
     */
    public void insertSysSerialOption(SysSerialConfig sysSerialConfig) {
        List<SysSerialOption> sysSerialOptionList = sysSerialConfig.getSysSerialOptionList();
        if (StringUtils.isNotEmpty(sysSerialOptionList)) {
            sysSerialConfigMapper.batchSysSerialOption(sysSerialConfig);
        }
    }
}
